package database.dao;
import java.sql.*;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
/**
 * @category 数据库的连接
 * @author chenkai
 *
 */
public class DBConnection {
public static Connection getConnection(){
		Connection con=null;
		String contextPath = ServletActionContext.getServletContext().getRealPath("/");
		String path = contextPath + "jdbc.properties";
		Map map = MysqlCrud.getPropertyValues(path);
		String ip = (String) map.get("ip");
		String user = (String) map.get("user");
		String password = (String) map.get("password");	
		try {		
			Class.forName("com.mysql.jdbc.Driver");
			System.out.println("数据库驱动加载成功");
			//con = DriverManager.getConnection("jdbc:mysql://localhost:3306/subject?user=root&password=110110&?useUnicode=true&characterEncoding=utf-8");
			con = DriverManager.getConnection("jdbc:mysql://"+ip+":3306/subject?user="+user+"&password="+password+"&?useUnicode=true&characterEncoding=utf-8");
			System.out.println("数据库连接成功");			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return con;
	}
}
